package com.goalias.util;


import com.goalias.constant.SystemConstants;
import com.goalias.exception.BizException;
import com.goalias.exception.ExceptionEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component
public class EmailUtil {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单文本邮件
     *
     * @param to      收件人
     * @param subject 邮件主题
     * @param text    邮件内容
     */
    @Async
    public void sendSimpleMail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       收件人
     * @param subject  邮件主题
     * @param text     邮件内容
     * @param filePath 附件路径
     * @throws MessagingException
     */
    @Async
    public void sendMailWithAttachment(String to, String subject, String text, String filePath){//文件存储方式？
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setValidateAddresses(true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            helper.addAttachment(Objects.requireNonNull(file.getFilename()), file);
        } catch (MessagingException e) {
            throw new BizException(0, "附件邮件发送失败");
        }

        mailSender.send(mimeMessage);
    }

    /**
     * 发送HTML格式邮件
     *
     * @param to      收件人
     * @param subject 邮件主题
     * @param html    HTML内容
     */
    @Async
    public void sendHtmlMail(String to, String subject, String html) {
        javax.mail.internet.MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setValidateAddresses(true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(html, true);
        } catch (MessagingException e) {
            throw new BizException(ExceptionEnum.EMAIL_ERROR);
        }
        mailSender.send(mimeMessage);
    }

    public String findPasswordTemplate(String accountName, String code, String mainUrl) {
        return "<body>" +
                "<div style='margin:0;'><span style='font-size: 20px;'><b>尊敬的用户：</b>" +
                "</span></div><div style='margin:0;'>&nbsp; &nbsp; &nbsp;您好！（登录账号：<b>" +
                accountName +
                "</b>）</div><div style='margin:0;'>您正在通过邮件重置密码，请点击" +
                "<a href='https://" + mainUrl + "/index?to=4&code=" + code
                + "' style='text-decoration:none;' taret='_blank'><b>这里</b></a>" +
                "跳转，" +
                "</div><div style='margin:0;'>或复制链接：<b>https://"
                + mainUrl + "/index?to=4&code=" + code
                + "</b></div><div style='margin:0;'><br></div><div style='margin:0;'>" +
                "如果您现在想起了您的密码：</div><div style='margin:0;'>" +
                "您可以忽略上述信息，可继续使用原来的密码登录，无需执行任何操作。" +
                "</div><div><span style='color: rgb(221, 64, 50);'><b>如果此邮件非您本人操作，您无需理会此邮件。" +
                "</b></span></div><div style='margin:0;'>这是一封系统自动发出的邮件，请不要直接回复。" +
                "</div><div style='margin:0;'><br></div><div style='margin:0;'><br></div></body>";
    }
    public String warningTemplate(String className, String methodName,String warnType) {
        return "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>系统告警通知</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "            background-color: #f4f4f4;\n" +
                "        }\n" +
                "        .email-container {\n" +
                "            width: 600px;\n" +
                "            margin: 50px auto;\n" +
                "            background-color: #ffffff;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "        .email-header {\n" +
                "            text-align: center;\n" +
                "            font-size: 24px;\n" +
                "            font-weight: bold;\n" +
                "            color: #333333;\n" +
                "            margin-bottom: 20px;\n" +
                "        }\n" +
                "        .email-body {\n" +
                "            font-size: 16px;\n" +
                "            color: #333333;\n" +
                "            line-height: 1.6;\n" +
                "        }\n" +
                "        .email-body p {\n" +
                "            margin: 10px 0;\n" +
                "        }\n" +
                "        .alert-section {\n" +
                "            background-color: #f9f9f9;\n" +
                "            border: 1px solid #e0e0e0;\n" +
                "            border-radius: 4px;\n" +
                "            padding: 15px;\n" +
                "            margin-top: 20px;\n" +
                "        }\n" +
                "        .alert-section h3 {\n" +
                "            margin: 0;\n" +
                "            font-size: 18px;\n" +
                "            color: #e74c3c;\n" +
                "        }\n" +
                "        .alert-details {\n" +
                "            margin-top: 10px;\n" +
                "        }\n" +
                "        .alert-details span {\n" +
                "            font-weight: bold;\n" +
                "        }\n" +
                "        .footer {\n" +
                "            text-align: center;\n" +
                "            font-size: 12px;\n" +
                "            color: #888888;\n" +
                "            margin-top: 30px;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"email-container\">\n" +
                "        <div class=\"email-header\">\n" +
                "            系统告警通知\n" +
                "        </div>\n" +
                "        <div class=\"email-body\">\n" +
                "            <p>尊敬的用户，</p>\n" +
                "            <p>以下是您系统的最新告警信息：</p>\n" +
                "\n" +
                "            <div class=\"alert-section\">\n" +
                "                <h3>告警详情</h3>\n" +
                "                <div class=\"alert-details\">\n" +
                "                    <p><span>类名:</span> "+className+"</p>\n" +
                "                    <p><span>方法名:</span> "+methodName+"</p>\n" +
                "                    <p><span>警告类型:</span> "+warnType+"</p>\n" +
                "                    <p>该"+warnType+"事件可能会影响您的服务正常运行，请尽快查看并采取相应措施。</p>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "\n" +
                "            <p>如果您有任何问题，请随时联系技术支持。</p>\n" +
                "        </div>\n" +
                "\n" +
                "        <div class=\"footer\">\n" +
                "            <p>感谢您的使用！</p>\n" +
                "            <p>此邮件为系统自动发送，请勿回复。</p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
    }

}
